#pragma once

#ifndef VTTIMAGE_H
#define VTTIMAGE_H

#include "VTTDataTypes.h"
#include "VTTMemoryAllocator.h"

#include <string>
#include <vector>

class VTTPackNode;

/**
  *	Image class whose instances represent images. It inherits from VTTMemoryAllocator
  * because it allocates memory on the heap to load the image data into memory.
  */
class VTTImage : public VTTMemoryAllocator
{

public:
	VTTImage( void );
	VTTImage( const std::string &strFilePath );
	VTTImage( const unsigned char *internalData,
		UINT32 width, UINT32 height, UINT32 numComponents );
	VTTImage( const VTTImage &image );

	virtual ~VTTImage( void );

	void AddPackNode( const VTTPackNode* packNode );
	bool CorrespondsToImageFile( void ) const;
	void ClearPackNodes( void );
	UINT32 DeleteInternalData( void );
	virtual UINT32 FreeAllocatedMemory( void );

	const unsigned char *GetDataPointer( void ) const;
	UINT32 GetHeight( void ) const;
	std::string GetImagePath( void ) const;
	const tm *GetLastChangingDate( void ) const;
	UINT32 GetNumComponents( void )const ;
	const std::vector<VTTPackNode*> &GetPackNodeVector( void ) const;
	VTTImage *GetSubImage( UINT32 x, UINT32 y, UINT32 width, UINT32 height ) const;
	UINT32 GetWidth( void ) const;
	
	
	bool Init( const std::string &strFilePath );
	bool Load( void );
	bool Reload( void );

	const tm *UpdateLastChangingDate( void );

	static bool GetNumPixels( const std::string &imagePath, UINT32 &numPixels );
	static bool GetSize( const std::string &imagePath, UINT32 &width, UINT32 &height );
	static bool WriteImageFile( const std::string &fileName, const unsigned char *data,
		UINT32 width, UINT32 height, UINT32 numComponents, UINT32 quality = 50 );

// 	static double timeForMemcpy;
// 	static double timeForLib;
// 	static double timeForMalloc;
// 	static double timeForDeletion;
// 
// 	static UINT32 filesRead;
// 	static UINT32 filesInMemory;

private:

	void InitMembers( void );

	/** Full path of the image, when it corresponds to an image file */
	std::string mImagePath;
	/** Vector with all the pack nodes of the image. A pack node stores the information about the location of the image inside the Atlas */
	std::vector<VTTPackNode*> mPackNodeVector;

	/** Image data */
	unsigned char *mInternalData;
	/** Pointers to the single rows of the image */
	unsigned char **mRowPointers;

	/** Height of the image in pixels */
	UINT32 mHeight;
	/** Number of components used for a single pixel, 3 for RGB */
	UINT32 mNumComponents;
	/** Width of the image in pixels */
	UINT32 mWidth;
	/** Color space of the image */
	VTTColorSpace mColorSpace;
	/** Image format of the image file */
	VTTImageFormat mUnderlyingImageFormat;
	/** Boolean value indicating whether the image corresponds to an image file or not */
	bool mCorrespondsToImageFile;
	
	/** Time stamp that stores the information of the last changing date of the image */
	struct tm mLastChangingDate;
};

#endif